using UnityEngine;

public class AutoDestroy : MonoBehaviour
{
    public float lifeTime = 60f; // 存活时间，以秒为单位
    private GameController gameController; // 引用 GameController 脚本

    private void Start()
    {
        gameController = GameObject.FindObjectOfType<GameController>(); // 获取 GameController 实例
        Invoke("DestroySelf", lifeTime);
    }

    private void DestroySelf()
    {
        // 从游戏对象名字中解析出用户名
        string[] nameParts = gameObject.name.Split('_');
        if (nameParts.Length == 2)
        {
            string usernameToRemove = nameParts[1];
            gameController.RemoveEnemyFromList(usernameToRemove); // 在销毁之前从列表中移除
        }

        Destroy(gameObject);
    }
}
